﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class product_saving_admin_saving_apply : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btSumit_Click(object sender, EventArgs e)
    {
        
        var gengerator = new AccountsManager();
        String saving_acc;
        String password = gengerator.GenerateRandomPass();

        if (this.ddlPlan.SelectedValue.Equals("PowerSave"))
        {
            saving_acc = gengerator.GenerateNo("SAVING");
            

        }
        else
        {
            saving_acc = gengerator.GenerateNo("KID_SAVING");
            
           
        }

        
        String cust_Id = gengerator.GenerateNo("CUSTOMER");

        
        string strpfName = tbfName.Text;
        string strplName = tblName.Text;
        string strpIC = tbIC.Text;
        string strOccupation = tbOccupation.Text;
        string strpDob = ddlDay.SelectedItem.Value + "/" + ddlMonth.SelectedItem.Value + "/" + ddlYear.SelectedItem.Value;
        string strMartial = ddlStatus.SelectedItem.Value; 
        string strpHome = tbHome.Text;
        string strpMobile = tbMobile.Text;
        string strpEmail = tbEmail.Text;
        string strAddress = tbAddress.Text;
        string strPostalCode = tbPostalCode.Text;
        string strGender = rblGender.SelectedItem.Value;
        string strNationality = tbNationality.Text;

        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);

        try
        {
        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandp = new SqlCommand("INSERT INTO CUSTOMER(custID, email, custPassword, fName, lName, custGender, custNRIC, custDob, custTel, custMobile, custAddress, custNationality, custOccupation, custPostal, custMarital)" +
        "VALUES (@id, @email, @password, @fname, @lname, @gender, @ic, @dob, @tel, @mobile, @address, @nationality, @occupation, @postal, @martial)", myConnection);
        myConnection.Open();
        myCommandp.Parameters.AddWithValue("@id", cust_Id);
        myCommandp.Parameters.AddWithValue("@email", tbEmail.Text);
        myCommandp.Parameters.AddWithValue("@password", password);
        myCommandp.Parameters.AddWithValue("@fname", tbfName.Text);
        myCommandp.Parameters.AddWithValue("@lname", tblName.Text);
        myCommandp.Parameters.AddWithValue("@gender", strGender);
        myCommandp.Parameters.AddWithValue("@ic", tbIC.Text);
        myCommandp.Parameters.AddWithValue("@dob", strpDob);
        myCommandp.Parameters.AddWithValue("@address", tbAddress.Text);
        myCommandp.Parameters.AddWithValue("@tel", tbHome.Text);
        myCommandp.Parameters.AddWithValue("@mobile", tbMobile.Text);
        myCommandp.Parameters.AddWithValue("@nationality", tbNationality.Text);
        myCommandp.Parameters.AddWithValue("@occupation", tbOccupation.Text);
        myCommandp.Parameters.AddWithValue("@postal", tbPostalCode.Text);
        myCommandp.Parameters.AddWithValue("@martial", strMartial);
        myCommandp.ExecuteNonQuery();

        SqlCommand myCommands = new SqlCommand("INSERT INTO ACCOUNTS(custID, accountNo, type) " +
        "VALUES (@custID, @accountNo,  @type)", myConnection);
        myCommands.Parameters.AddWithValue("@custID", cust_Id);
        myCommands.Parameters.AddWithValue("@accountNo", saving_acc);
        myCommands.Parameters.AddWithValue("@type", "SAVING");
        myCommands.ExecuteNonQuery();

        myCommands = new SqlCommand("INSERT INTO SAVING_ACCOUNT(accountNo,custID, balance, interestRate, effectiveDate, savingType) " +
       "VALUES (@accNo,@custID, @balance, @iRate, @eDate, @type)", myConnection);
        myCommands.Parameters.AddWithValue("@accNo", saving_acc);
        myCommands.Parameters.AddWithValue("@custID", cust_Id);
        myCommands.Parameters.AddWithValue("@balance", 0);
        myCommands.Parameters.AddWithValue("@iRate",0);
        myCommands.Parameters.AddWithValue("@eDate", DateTime.Now);
        myCommands.Parameters.AddWithValue("@type", ddlPlan.SelectedItem.Value);
        myCommands.ExecuteNonQuery();
        
        if (this.ddlPlan.SelectedValue.Equals("PowerSave"))
        {
           
            Response.Redirect("~/product/saving/admin/saving_afterformA.aspx");

        }
        else
        {
            
            Response.Redirect("~/product/saving/admin/saving_afterformK.aspx");

        }

        myConnection.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
        Response.Write(ex);
        }
        finally
        {
        myConnection.Close();
        }
        }

   
        
    
}
